//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ty.pdm.system.workFlow.listener.common;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.jsyxdl.k3cloud.listener.K3CloudHttpService;
import com.jsyxdl.k3cloud.listener.WorkFlowObjectPackageRoutine;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.impl.el.FixedValue;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;


@CustomListener(
        category = "金蝶k3cloud任务与执行监听器",
        description = "永续变更流程ERP通用接口"
)
public class ChangesMaterialApplicationListener implements ExecutionListener, TaskListener {
    private static final Logger log = LoggerFactory.getLogger(ChangesMaterialApplicationListener.class);
    private static final long serialVersionUID = -9059663559349037414L;

    @CustomListenerField(
            description = "ERP接口编码",
            defaultValue = "",
            required = true
    )
    private FixedValue code;

    @CustomListenerField(
            description = "ERP接口对象",
            defaultValue = "",
            required = true
    )
    private FixedValue erpObject;
    @CustomListenerField(
            description = "物料已存在是否修改",
            defaultValue = "",
            required = true
    )
    private FixedValue check;
    @CustomListenerField(
            description = "物料批量ERP保存接口编码",
            defaultValue = "",
            required = true
    )
    private FixedValue materialsCode;

    @CustomListenerField(
            description = "物料批量ERP保存接口对象",
            defaultValue = "",
            required = true
    )
    private FixedValue materialsObject;

    @CustomListenerField(
            description = "替代方案ERP保存接口编码",
            defaultValue = "",
            required = true
    )
    private FixedValue alternativeCode;

    @CustomListenerField(
            description = "全局替代接口对象",
            defaultValue = "",
            required = true
    )
    private FixedValue globalObject;

    @CustomListenerField(
            description = "局部替代接口对象",
            defaultValue = "",
            required = true
    )
    private FixedValue localityObject;

    @CustomListenerField(
            description = "替代方案反审核接口编码",
            defaultValue = "",
            required = true
    )
    private FixedValue unAuditCode;


    @CustomListenerField(
            description = "替代方案反审核接口对象",
            defaultValue = "",
            required = true
    )
    private FixedValue unAuditObject;

    public ChangesMaterialApplicationListener() {

    }

    @Override
    public void notify(DelegateExecution execution) {
        try {
            String workFlowContainerIdStr = execution.getProcessInstanceBusinessKey();
            BigInteger workFlowContainerId = new BigInteger(workFlowContainerIdStr);
            TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                    TyplmWorkFlowContainerService.class);
            if (typlmWorkFlowContainerService != null) {
                WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                        workFlowContainerId);
                if (workFlowContainerView instanceof WorkFlowObjectPackage) {
                } else if (workFlowContainerView instanceof FlowChgFormView) {
                    FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
                    WorkFlowObjectPackageRoutine workFlowObjectPackageRoutine = new WorkFlowObjectPackageRoutine();
                    String check="";
                    if(this.check!=null){
                        check=this.check.getExpressionText();
                    }

                    JSONObject obj = workFlowObjectPackageRoutine.erpChangeTaskWorkderRoutine(
                            check,
                            this.code.getExpressionText(),
                            this.erpObject.getExpressionText(),
                            this.materialsCode.getExpressionText(),
                            this.materialsObject.getExpressionText(),
                            this.alternativeCode.getExpressionText(),
                            this.globalObject.getExpressionText(),
                            this.localityObject.getExpressionText(),
                            "FSH010032160D", "替代方案",
                            flowChgFormView);

                    if (obj != null) {
                        JSONObject Result = obj.getJSONObject("Result");
                        if (Result != null) {
                            JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                            if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                        ResponseStatus.get("Errors").toString());
                            }

                        } else {
                            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("返回ERP结构Result为空");
                        }
                    } else {
                        throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("返回结构为空");
                    }
                } else {
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("WorkFlowObjectPackage结构错误");
                }
            } else {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("TyplmWorkFlowContainerService无法加载");
            }

        } catch (Exception var5) {
            log.error("监听ChangesMaterialApplicationListener异常：", var5);
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(var5.getMessage());
        }
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        try {
            BigInteger workFlowContainerId = new BigInteger(delegateTask.getVariable("workFlowContainerId").toString());
            TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                    TyplmWorkFlowContainerService.class);
            if (typlmWorkFlowContainerService != null) {
                WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                        workFlowContainerId);
                if (workFlowContainerView instanceof WorkFlowObjectPackage) {
                } else if (workFlowContainerView instanceof FlowChgFormView) {
                    FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
                    WorkFlowObjectPackageRoutine workFlowObjectPackageRoutine = new WorkFlowObjectPackageRoutine();
                    String check="";
                    if(this.check!=null){
                        check=this.check.getExpressionText();
                    }
                    JSONObject obj = workFlowObjectPackageRoutine.erpChangeTaskWorkderRoutine(
                            check,
                            this.code.getExpressionText(),
                            this.erpObject.getExpressionText(),
                            this.materialsCode.getExpressionText(),
                            this.materialsObject.getExpressionText(),
                            this.alternativeCode.getExpressionText(),
                            this.globalObject.getExpressionText(),
                            this.localityObject.getExpressionText(),
                            "FSH010032160D", "替代方案",
                            flowChgFormView);

                    if (obj != null) {
                        JSONObject Result = obj.getJSONObject("Result");
                        if (Result != null) {
                            JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                            if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                        ResponseStatus.get("Errors").toString());
                            }
                        } else {
                            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("返回ERP结构Result为空");
                        }
                    } else {
                        throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("返回结构为空");
                    }

                } else {
                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("WorkFlowObjectPackage结构错误");
                }
            } else {
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("TyplmWorkFlowContainerService无法加载");
            }

        } catch (Exception var5) {
            log.error("监听ChangesMaterialApplicationListener异常：", var5);
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(var5.getMessage());

        }
    }
}
